package com.wl.controller;

import com.mysql.cj.util.StringUtils;
import com.wl.pojo.Orders;
import com.wl.response.CommonReturnType;
import com.wl.service.OrderService;
import com.wl.service.impl.OrderServiceImpl;
import com.wl.util.MyUtil;
import com.wl.util.RedisUtil;
import lombok.SneakyThrows;
import redis.clients.jedis.Jedis;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * @program: treeHole
 * @author: 吴磊
 * @create: 2021-07-07 10:10
 */
@WebServlet("/pay.action")
public class PayServlet extends BaseServlet {

    private OrderService orderService = new OrderServiceImpl();


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String op = request.getParameter("op");
        if (StringUtils.isNullOrEmpty(op)) {
            show404Common(request, response);
            return;
        }
        if ("getQrCode".equals(op)) {
            getQrCodeOp(request, response);
        } else if ("notifyUrl".equals(op)) {
            notifyUrlOp(request, response);
        } else if ("orderIsPay".equals(op)) {
            orderIsPayOp(request, response);
        } else if ("getAllOrder".equals(op)) {
            getAllOrderOp(request, response);
        } else if ("deleteOrder".equals(op)) {
            deleteOrderOp(request, response);
        } else {
            show404Common(request, response);
        }
    }

    /**
     * 管理员删除支付订单
     *
     * @param request
     * @param response
     */
    private void deleteOrderOp(HttpServletRequest request, HttpServletResponse response) {
        boolean b = orderService.deleteOrder(request);
        if (!b) {
            MyUtil.responseData(response, CommonReturnType.fail("删除失败"));
        } else {
            MyUtil.responseData(response, CommonReturnType.success(""));
        }
    }

    /**
     * 获取所有的订单
     *
     * @param request
     * @param response
     */
    private void getAllOrderOp(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = orderService.getAllOrder(request);
        if (map == null || map.size() <= 0) {
            MyUtil.responseData(response, CommonReturnType.fail("没有支付订单"));
        } else {
            MyUtil.responseData(response, CommonReturnType.success(map));
        }
    }

    /**
     * 检查订单是否支付成功
     *
     * @param request
     * @param response
     */
    @SneakyThrows
    private void orderIsPayOp(HttpServletRequest request, HttpServletResponse response) {
        String orderId = request.getParameter("orderId");
        Jedis jedis = RedisUtil.getJedis();
        String redisOrderId = jedis.get(orderId);
        if (StringUtils.isNullOrEmpty(redisOrderId)) {
            MyUtil.responseData(response, CommonReturnType.success(true));
        } else {
            MyUtil.responseData(response, CommonReturnType.success(false));
        }
        jedis.close();
    }

    /**
     * 接口异步回调
     *
     * @param request
     * @param response
     */
    @SneakyThrows
    private void notifyUrlOp(HttpServletRequest request, HttpServletResponse response) {
        String string = orderService.notifyUrl(request);
        PrintWriter writer = response.getWriter();
        writer.println(string);
    }

    /**
     * 获取二维码
     *
     * @param request
     * @param response
     */
    @SneakyThrows
    private void getQrCodeOp(HttpServletRequest request, HttpServletResponse response) {
        Orders orders = new Orders();
        int orderAmount = Integer.parseInt(request.getParameter("orderAmount") == null ? "0" : request.getParameter("orderAmount"));
        orders.setOrderAmount(orderAmount);
        Map<String, Object> map = orderService.FaceTofACEPay(request, orders);
        MyUtil.responseData(response, CommonReturnType.success(map));
    }
}
